package com.example.qjia.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.qjia.R;
import com.example.qjia.data.romotedata.Teacher;

import java.util.List;

/**
 * Created by ying_xiang on 2016/8/31.
 */
public class TeachersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private List<Teacher> teachers;
    private Context mContext;
    private TeacherChooseListener teacherChooseListener;

    public void setTeacherChooseListener(TeacherChooseListener teacherChooseListener) {
        this.teacherChooseListener = teacherChooseListener;
    }

    public TeachersAdapter(List<Teacher> teachers, Context mContext) {
        this.teachers = teachers;
        this.mContext = mContext;
    }

    @Override
    public void onClick(View view) {
        Teacher teacher=(Teacher) view.getTag();//获取老师的名字
        String tname=teacher.getName();
        int tid=(Integer)teacher.getTid();//获取老师的id
        System.out.println("老师的名字和id"+tname+"--"+tid);
        teacherChooseListener.setTeacher(tname,tid);

    }

    public interface TeacherChooseListener{

        public void  setTeacher(String teacherName,int tid);

    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.teacher_dialog_item, parent, false);
        return new ItemHolder(view) ;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ItemHolder itemHolder=(ItemHolder)holder;
        itemHolder.teacherName.setText(teachers.get(position).getName());
        itemHolder.teacherItem.setTag(teachers.get(position));

        itemHolder.teacherItem.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return teachers.size();
    }
    static class ItemHolder extends RecyclerView.ViewHolder{


        TextView teacherName;
        LinearLayout teacherItem;
        public ItemHolder(View itemView) {
            super(itemView);
            teacherName=(TextView)itemView.findViewById(R.id.teacherName);
            teacherItem=(LinearLayout)itemView.findViewById(R.id.teacherItem);



        }
    }

}
